package com.gewu.bookeeping.View.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.gewu.bookeeping.MainActivity;
import com.gewu.bookeeping.MyDb.bean.UserBeanIn;
import com.gewu.bookeeping.MyDb.dao.UserDaoPerson;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.Utils.AppCot;
import com.google.android.material.textfield.TextInputLayout;

import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private TextInputLayout name;
    private TextInputLayout pass;
    private Button login;
    private Button zhuce;
    private UserBeanIn userBeanIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppCot.isno(LoginActivity.this)) {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        } else {
            setContentView(R.layout.activity_login);
            initView();
        }
    }

    private void initView() {
        name = findViewById(R.id.name);
        pass = findViewById(R.id.pass);
        login = (Button) findViewById(R.id.login);
        zhuce = (Button) findViewById(R.id.zhuce);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name.getEditText().getText().length() > 0 && name.getEditText().getText().length() > 0) {
                    UserDaoPerson userDaoPerson = new UserDaoPerson(LoginActivity.this);
                    List<UserBeanIn> quer = userDaoPerson.querLike(name.getEditText().getText().toString());
                    if (quer == null) {
                        Toast.makeText(LoginActivity.this, "暂时没有用户", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    userBeanIn = quer.get(0);
                    if (LoginActivity.this.userBeanIn.getName().equals(name.getEditText().getText().toString()) && LoginActivity.this.userBeanIn.getPwa().equals(pass.getEditText().getText().toString())) {
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        AppCot.isok(LoginActivity.this, true);
                        AppCot.name = userBeanIn.getName();
                        finish();
                    } else {
                        AppCot.showDilog(LoginActivity.this, "提示", "登陆失败").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                name.getEditText().setText(null);
                                pass.getEditText().setText(null);
                                dialog.dismiss();
                            }
                        }).show();
                    }
                }
            }
        });

        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name.getEditText().getText().length() > 0 && name.getEditText().getText().length() > 0) {
                    UserDaoPerson userDaoPerson = new UserDaoPerson(LoginActivity.this);
                    List<UserBeanIn> quer = userDaoPerson.querLike(name.getEditText().getText().toString());
                    if (quer==null){
                        UserBeanIn userBeanIn = new UserBeanIn();
                        userBeanIn.setName(name.getEditText().getText().toString());
                        userBeanIn.setPwa(pass.getEditText().getText().toString());
                        int add = userDaoPerson.add(userBeanIn);
                        AppCot.showDilog(LoginActivity.this, "提示", "注册成功").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                name.getEditText().setText(null);
                                pass.getEditText().setText(null);
                                dialog.dismiss();
                            }
                        }).show();
                    }

                    if (quer.size()>1){
                        Toast.makeText(LoginActivity.this, "账号名存在", Toast.LENGTH_SHORT).show();
                    }else {
                        UserBeanIn userBeanIn = new UserBeanIn();
                        userBeanIn.setName(name.getEditText().getText().toString());
                        userBeanIn.setPwa(pass.getEditText().getText().toString());
                        int add = userDaoPerson.add(userBeanIn);
                        AppCot.showDilog(LoginActivity.this, "提示", "注册成功").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                name.getEditText().setText(null);
                                pass.getEditText().setText(null);
                                dialog.dismiss();
                            }
                        }).show();
                    }

                }
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return false;
    }
}